package sunsun.xiaoli.jiarebang.utils.audiorecord;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.Handler;
import android.util.Log;
import com.itboye.pondteam.utils.LogUtils;

public class AudioPlayUtil {

    private final String TAG = "AudioRecorder";
    AudioTrack audioTrack = null;

    public AudioTrack getAudioTrack() {
        return audioTrack;
    }

    public void setAudioTrack(AudioTrack audioTrack) {
        this.audioTrack = audioTrack;
    }

    public void play(byte[] buffer, IPlayStatus playFinish) {
        int bufferSize = AudioTrack.getMinBufferSize(16000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
        audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 16000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM);
        audioTrack.setNotificationMarkerPosition(buffer.length);
        audioTrack.play();
        audioTrack.write(buffer, 0, buffer.length);
        audioTrack.setPlaybackPositionUpdateListener(new AudioTrack.OnPlaybackPositionUpdateListener() {
            @Override
            public void onMarkerReached(AudioTrack audioTrack) {
                LogUtils.v(TAG, audioTrack.getPlayState() + " onMarkerReached");
            }

            @Override
            public void onPeriodicNotification(AudioTrack audioTrack) {
                LogUtils.v(TAG, audioTrack.getPlayState() + " onPeriodicNotification");
                playFinish.playFinish(0);
                stop();
            }
        });
        audioTrack.stop();
        audioTrack.release();
        playFinish.playFinish(0);
    }

    public void setOnCompleteListener(String url, int position, IPlayStatus playFinish) {
        if (audioTrack != null) {
            audioTrack.setPlaybackPositionUpdateListener(new AudioTrack.OnPlaybackPositionUpdateListener() {
                @Override
                public void onMarkerReached(AudioTrack audioTrack) {
                    LogUtils.v(TAG, audioTrack.getPlayState() + " onMarkerReached");
                }

                @Override
                public void onPeriodicNotification(AudioTrack audioTrack) {
                    LogUtils.v(TAG, audioTrack.getPlayState() + " onPeriodicNotification");
                    playFinish.playFinish(position);
                    stop();
                }
            },new Handler());
//            audioTrack.setPlaybackPositionUpdateListener(new AudioTrack.OnPlaybackPositionUpdateListener() {
//                @Override
//                public void onMarkerReached(AudioTrack audioTrack) {
//                    LogUtils.v(TAG, audioTrack.getPlayState() + " onMarkerReached");
//                }
//
//                @Override
//                public void onPeriodicNotification(AudioTrack audioTrack) {
//                    LogUtils.v(TAG, audioTrack.getPlayState() + " onPeriodicNotification");
//                    playFinish.playFinish(position);
//                    stop();
//                }
//            });

        }
    }

    public interface IPlayStatus {
        void playFinish(int position);
    }

    public void pause() {
        if (audioTrack != null) {
            audioTrack.pause();
        }
    }

    public void stop() {
        if (audioTrack != null) {
            audioTrack.stop();
            audioTrack.release();
            audioTrack = null;
        }
    }

}
